﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace SoFileManager
{
    /// <summary>
    /// 文件按日期归档
    /// </summary>
    /// <remarks>2014-5-8</remarks>
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
            btnBrowser.Click += new EventHandler(btn_Click);
            btnRun.Click += new EventHandler(btn_Click);
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
        }
        private void btn_Click(object sender, EventArgs e)
        {
            switch (((Button)sender).Name)
            {
                case "btnBrowser":
                    FolderBrowserDialog fbd = new FolderBrowserDialog();
                    if (DialogResult.OK != fbd.ShowDialog()) return;
                    tbPath.Text = fbd.SelectedPath;
                    if (tbOutput.TextLength == 0) tbOutput.Text = tbPath.Text + "\\SoOutput";
                    break;
                case "btnRun":
                    if (tbPath.TextLength == 0 || !Directory.Exists(tbPath.Text) || tbOutput.TextLength == 0) return;
                    RenameFiles(tbPath.Text, tbOutput.Text);
                    break;
            }
        }

        //文件归类
        private void RenameFiles(string path, string output)
        {
            int iexists = 0, ido = 0, ipass = 0;
            string dir, fn;
            DateTime dtm;
            StringBuilder sbOut = new StringBuilder();
            IEnumerable<string> files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories);
            foreach (string fpath in files)
            {
                if (fpath.StartsWith(output, StringComparison.OrdinalIgnoreCase))
                {
                    ipass++;
                    continue;
                }
                dtm = File.GetLastWriteTime(fpath);
                dir = output + "\\" + dtm.ToString("yyyy_MM_dd");
                if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
                fn = dir + "\\" + Path.GetFileName(fpath);
                if (File.Exists(fn))
                {
                    iexists++;
                    sbOut.AppendLine(fpath);
                }
                else
                {
                    ido++;
                    File.Move(fpath, fn);
                }
            }
            tbResult.Text = string.Format("文件归类完成! [共移动{0}个文件,重复{1}个文件]{2}==========={4}========={2}{3}", ido, iexists, Environment.NewLine, sbOut.ToString(), sbOut.Length == 0 ? "==================" : "以下文件重复");
        }
        private string ReRenameFile(string frompath, string toPathWithOutExt, string ext, ref int i)
        {
            if (File.Exists(toPathWithOutExt + ext))
            {
                i++;
                return ReRenameFile(frompath, toPathWithOutExt + i, ext, ref i);
            }
            else
            {
                File.Move(frompath, toPathWithOutExt + ext);
                return toPathWithOutExt + ext;
            }
        }
    }
}
